package com.zretc.letter.controller;

import com.zretc.api.pojo.Letter;
import com.zretc.common.constant.SecurityConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.letter.service.LetterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/letter")
public class LetterController {
//    @GetMapping("sayhello")
//    public RespResult sayhello(HttpServletRequest req)
//    {
//        letterService.sayHello("aaaaaaaaaaa");
//        return R.success(req.getHeader("userId"));
//    }
    @Autowired
    private LetterService letterService;
    @GetMapping("/queryLettersTitleAndDateByAmount")
    public RespResult queryLettersTitleAndDateByAmount(@RequestParam("index")Integer index, @RequestParam("pageNum")Integer pageNum,
                                                       HttpServletRequest req)
    {
        Integer userId= Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY));
        List<Letter> list= letterService.queryLettersByAmount(index, pageNum,userId);
        if(CollectionUtils.isEmpty(list))return R.fail(500,"服务器未找到数据或异常");
        List<Map<String, Object>> result = list.stream()
                .map(obj -> {
                    Map<String, Object> map = new HashMap<>();
                    // 假设MyObject类有getName和getAge方法
                    map.put("id", obj.getId());
                    map.put("title", obj.getTitle());
                    map.put("isRead", obj.isRead());
                    map.put("createdAt", obj.getCreatedAt());
                    // 为其他属性添加键值对
                    return map;
                })
                .collect(Collectors.toList());
        return R.success(result);
    }
    @GetMapping("/queryLetterById")
    public RespResult queryLetterById(@RequestParam("id")Integer id,HttpServletRequest req)
    {
        Letter letter= letterService.queryLetterById(id, Integer.valueOf
                (req.getHeader(SecurityConstant.AUTHENTICATION_KEY)));
        if(Objects.isNull(letter))return R.fail(500,"服务器错误");
        return R.success(letter);
    }
    @PostMapping("/sendLettersToAll")
    public RespResult sendLettersToAll(@RequestBody Letter letter)
    {
        if(!letterService.publishLetterToAll(letter))return R.fail(500,"服务器错误");
        return R.success(null);
    }
    /**
     * @param letter 实体类信
     * @param userId 发送信人的id
     */
    @PostMapping("/sendLettersToSingleUser/{userId}")
    public RespResult sendLettersToSingleUser(@RequestBody Letter letter,@PathVariable Integer userId)
    {
        if(!letterService.publishLetterToSingleUser(letter,userId))return R.fail(500,"服务器错误");
        return R.success(null);
    }
    @PostMapping("/userDeleteLetter")
    public RespResult userDeleteLetter(@RequestBody Map<String,Object> map,HttpServletRequest req)
    {
        Integer id= (Integer) map.get("id");
        Integer userId= Integer.valueOf(req.getHeader(SecurityConstant.AUTHENTICATION_KEY));
        if(!letterService.deleteSingleLetterOfUser(userId,id))return R.fail(500,"服务器错误");
        return R.success(null);
    }

}
